﻿using BlogManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlogManager.Service
{
    public class PostRepository : IPostRepository
    {
        private static List<Post> Posts = new List<Post>();
        private static int _nextId = 1;

        private IComentarioRepository comentarioRepository = new ComentarioRepository();

        public PostRepository()
        {
            //Add(new Post { DateHora = DateTime.Now, Autor = "Paulo", Categoria = "Novo", Conteudo = "Nada" });
            //Add(new Post { DateHora = DateTime.Now, Autor = "Ingrid", Categoria = "Carro", Conteudo = "Palio" });
        }

        public List<Post> GetAll()
        {
            return Posts;
        }

        public Post Get(int id)
        {
            return Posts.Find(p => p.Id == id);
        }

        public Post Add(Post item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            item.Id = _nextId++;
            Posts.Add(item);
            return item;
        }

        public void Remove(int id)
        {
            Posts.RemoveAll(p => p.Id == id);
            comentarioRepository.RemoveByIdpost(id);
        }

        public bool Update(Post item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            int index = Posts.FindIndex(p => p.Id == item.Id);
            if (index == -1)
            {
                return false;
            }
            Posts.RemoveAt(index);
            Posts.Add(item);
            return true;
        }
    }
}